package com.lancoo.edu.platform.operation.backupRestore.service.timer.job;

import cn.hutool.core.collection.CollectionUtil;

import com.lancoo.edu.platform.operation.backupRestore.service.bo.BrBackupBoService;
import com.lancoo.edu.platform.operation.backupRestore.service.timer.ScheduleTask;
import com.lancoo.edu.platform.operation.commons.domain.api.system.ProductInfo;
import com.lancoo.edu.platform.operation.commons.service.api.YunBaseApiService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author lzh
 * @date 2024/6/21 17:21
 * @description 备份数据库任务
 */
@AllArgsConstructor
@Slf4j
public class BackUpDatabaseJob implements Job {


    private final YunBaseApiService yunBaseApiService;
    private final BrBackupBoService brBackupBoService;
    private final ScheduleTask scheduleTask;

    /*
     * 执行任务
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 1、获取触发器名（任务ID）
        String group = context.getTrigger().getKey().getGroup();
        String taskId = context.getTrigger().getKey().getName();
        if (scheduleTask.beExecute(group, taskId)) {
            return;
        }
        ProductInfo productInfo = yunBaseApiService.getProductInfo();
        if (Objects.isNull(productInfo) || StringUtils.isEmpty(productInfo.getEduId())){
            log.error("获取产品初始化信息失败");
            return;
        }
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        // 备份
        brBackupBoService.backupAll(productInfo.getEduId());
        stopWatch.stop();
        log.info("【备份耗时：{}】", stopWatch.getLastTaskTimeMillis());
    }
}
